package com.lxmes.base.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.base.domain.NcTechRoute;
import com.lxmes.base.service.INcTechRouteService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工艺路线Controller
 *
 * @author ruoyi
 * @date 2023-03-06
 */
@RestController
@RequestMapping("/base/techRoute")
public class NcTechRouteController extends BaseController
{
    @Autowired
    private INcTechRouteService ncTechRouteService;

    /**
     * 查询工艺路线列表
     */
    @PreAuthorize("@ss.hasPermi('base:techRoute:list')")
    @GetMapping("/list")
    public TableDataInfo list(NcTechRoute ncTechRoute)
    {
        startPage();
        List<NcTechRoute> list = ncTechRouteService.selectNcTechRouteList(ncTechRoute);
        return getDataTable(list);
    }

    /**
     * 导出工艺路线列表
     */
    @PreAuthorize("@ss.hasPermi('base:techRoute:export')")
    @Log(title = "工艺路线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NcTechRoute ncTechRoute)
    {
        List<NcTechRoute> list = ncTechRouteService.selectNcTechRouteList(ncTechRoute);
        ExcelUtil<NcTechRoute> util = new ExcelUtil<NcTechRoute>(NcTechRoute.class);
        util.exportExcel(response, list, "工艺路线数据");
    }

    /**
     * 获取工艺路线详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:techRoute:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ncTechRouteService.selectNcTechRouteById(id));
    }

    /**
     * 新增工艺路线
     */
    @PreAuthorize("@ss.hasPermi('base:techRoute:add')")
    @Log(title = "工艺路线", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NcTechRoute ncTechRoute)
    {
        return toAjax(ncTechRouteService.insertNcTechRoute(ncTechRoute));
    }

    /**
     * 修改工艺路线
     */
    @PreAuthorize("@ss.hasPermi('base:techRoute:edit')")
    @Log(title = "工艺路线", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NcTechRoute ncTechRoute)
    {
        return toAjax(ncTechRouteService.updateNcTechRoute(ncTechRoute));
    }

    /**
     * 删除工艺路线
     */
    @PreAuthorize("@ss.hasPermi('base:techRoute:remove')")
    @Log(title = "工艺路线", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ncTechRouteService.deleteNcTechRouteByIds(ids));
    }

    /**
     * 导入工艺路线
     */
    @Log(title = "工艺路线", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<NcTechRoute> util = new ExcelUtil<>(NcTechRoute.class);
        List<NcTechRoute> ncTechRouteList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ncTechRouteService.imporIteminfo(ncTechRouteList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    // 下载模板
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<NcTechRoute> util = new ExcelUtil<>(NcTechRoute.class);
        util.importTemplateExcel(response,"工艺路线");
    }
}
